package cn.wwcyc.controller;

import cn.wwcyc.domain.R;
import cn.wwcyc.domain.TjDevtype;
import cn.wwcyc.domain.TjSpecialtype;
import cn.wwcyc.mapper.TjDevtypeMapper;
import cn.wwcyc.mapper.TjSpecialtypeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/specialtype")

public class SPECIALTYPEController {

    //    @Autowired
//    TjDevtypeMapper tjDevtypeMapper;
    @Autowired
    TjSpecialtypeMapper tjSpecialtypeMapper;

    @RequestMapping("/getAll")
    public R<List<TjSpecialtype>> getAll(

    ) {
        List<TjSpecialtype> list = tjSpecialtypeMapper.selectList(null);
        return R.success(list);
    }

    @RequestMapping("/getListBydevtypeid")
    public R<List<TjSpecialtype>> getListBydevtypeid(
            @RequestParam Long devtypeid
    ) {
        QueryWrapper<TjSpecialtype> qw = new QueryWrapper<>();
        qw.eq("devtypeid", devtypeid);
        List<TjSpecialtype> list = tjSpecialtypeMapper.selectList(qw);
        return R.success(list);
    }

    //此分页函数中的返回对象中不含有单位及类型对象数据
    @RequestMapping("/getpage")
    public R<IPage<TjSpecialtype>> getPage1(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long devtypeid,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer funcode
    ) {
        LambdaQueryWrapper<TjSpecialtype> qw = new LambdaQueryWrapper();
        if (!StringUtils.isBlank(name))
            qw.like(TjSpecialtype::getName, name);
        if (devtypeid != null)
            qw.eq(TjSpecialtype::getDevtypeid, devtypeid);
        if (funcode != null)
            qw.eq(TjSpecialtype::getFuncode, funcode);
        IPage<TjSpecialtype> page = new Page<>(pagenum, pagesize);
        page = tjSpecialtypeMapper.selectPage(page, qw);
        return R.success(page);
    }


    @PostMapping("/save")
    public R save(@RequestBody TjSpecialtype specialtype) {
        tjSpecialtypeMapper.insert(specialtype);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody TjSpecialtype devtype) {
        tjSpecialtypeMapper.updateById(devtype);
        return R.success();
    }

    //
    @DeleteMapping
    public R del(Integer id) {
        tjSpecialtypeMapper.deleteById(id);
        return R.success();
    }

    @PostMapping("delbatch")
    public R delbatch(@RequestBody List<TjSpecialtype> list) {
        for (TjSpecialtype specialtype : list) {
            Long id = specialtype.getId();
            tjSpecialtypeMapper.deleteById(id);
        }
        return R.success();
    }
}
